/* task板块的reducer */
import { TASK_LIST, TASK_REMOVE, TASK_UPDATE } from '../action-types'

const initial = {
    taskList: null
}
export default function (state = initial, action) {
    state = { ...state }
    let { taskList } = state;
    switch (action.type) {
        case TASK_LIST:
            state.taskList = action.list;
            break;
        case TASK_UPDATE:
            if (Array.isArray(taskList)) {
                state.taskList = taskList.map(item => {
                    if (+item.id === +action.id) {
                        item.state = 2;
                        item.complete = new Date().toLocaleString('zh-CN')
                    }
                    return item
                })
            }
            break;
        case TASK_REMOVE:
            if (Array.isArray(taskList)) {
                state.taskList = taskList.filter((item) => {
                    return +item.id !== +action.id
                })
            }
            break;
        default:
    }
    return state;
}